package com.miniapp.controller;

import java.util.List;

import com.google.common.collect.Lists;
import com.miniapp.dto.SearchFieldDTO;
import com.miniapp.entity.MiniProductPriceEntity;
import com.miniapp.entity.MiniUserEntity;
import com.miniapp.service.MiniProductPriceService;
import com.miniapp.service.MiniUserService;
import com.miniapp.utils.RenderHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.miniapp.dto.PageDTO;
import com.miniapp.dto.SimpleSearchDTO;

import com.miniapp.entity.MiniUserProductEntity;

import com.miniapp.service.MiniUserProductService;
import com.miniapp.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 用户产品
 *
 * @author linyuchi
 * @email xunli-03@163.com
 * @date 2018-05-20 15:26:47
 */
@RestController
@RequestMapping("miniapp/miniuserproduct")
public class MiniUserProductController extends AbstractController{
    @Autowired
    private MiniUserProductService miniUserProductService;
    @Autowired
    private MiniProductPriceService miniProductPriceService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(){

        SimpleSearchDTO simpleSearchDTO = getSearchDtoFromJqGrid();
        PageDTO<MiniUserProductEntity> page = miniUserProductService.findPageBySql(simpleSearchDTO);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){

        MiniUserProductEntity miniUserProduct = miniUserProductService.getById(id);
        List<MiniProductPriceEntity> priceEntities = miniProductPriceService.findByProductId(miniUserProduct.getProductId());

        return R.ok().put("miniUserProduct", miniUserProduct).put("productPrice",priceEntities).put("username",this.getUser().getUsername());
    }

    /**
     * 续费
     * @param miniUserProduct
     * @return
     */
    @PostMapping("/renew")
    public R renew(@RequestBody MiniUserProductEntity miniUserProduct, HttpServletRequest request){

        return  miniUserProductService.updateRenew(miniUserProduct,request,this.getUserId());
    }

    /**
     * 产品升级
     */
    @PostMapping("/upgrade")
    public R upgrade(@RequestBody Long[] ids){

        return  miniUserProductService.upgrade(ids);
    }


}
